package com.tceasy.gateway.controller;

import com.tceasy.gateway.aop.Check;
import com.tceasy.gateway.api.Response;
import com.tceasy.gateway.domain.OpenDeveloper;
import com.tceasy.gateway.domain.OpenPermission;
import com.tceasy.gateway.service.DeveloperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by zhanghuan on 2016/4/11.
 * 开发者相关操作
 */
@Check
@Controller
@RequestMapping(value = "/developer",produces ={ "application/json;charset=UTF-8" })
public class DeveloperController {

    @Autowired
    DeveloperService developerService;

    /**
     * 添加账户
     * @param developer
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(OpenDeveloper developer){
        developerService.add(developer);
        return Response.SUCCESS("添加成功").toJson();
    }

    /**
     * 关闭账户
     * @param partner
     * @return
     */
    @RequestMapping("/stop")
    @ResponseBody
    public Object stop(String partner){
        developerService.stop(partner);
        return Response.SUCCESS("已关闭").toJson();
    }

    /**
     * 开启账户
     * @return
     */
    @RequestMapping("/open")
    @ResponseBody
    public Object open(String partner){
        developerService.open(partner);
        return Response.SUCCESS("已开启").toJson();
    }

    @RequestMapping("/updateUI/{method:.+}")
    public ModelAndView updateUI(@PathVariable String method, ModelAndView view){
        OpenDeveloper openDeveloper=developerService.findOne(method);
        view.addObject("openDeveloper",openDeveloper);
        view.setViewName("developer/update");
        return view;
    }

    /**
     * 更新记录
     * @param partner
     * @param developer
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Object update(String partner,OpenDeveloper developer){
        developerService.update(partner, developer);
        return Response.SUCCESS("修改成功").toJson();
    }

    /**
     * 分页查询
     * @param map 查询条件和排序条件
     *  matcher 查询条件，json格式，eg:{partner:1212,name:xxx,email:xxxx@web.com}
     *  orderBy 排序（可为空）允许多个字段参与排序，1代表正序，-1代表倒序，eg:{partner:-1,createDate:-1}
     * @param start 指定开始行数，可为空,默认首页（固定每页20条）
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam Map<String,String> map,@RequestParam(required = false,defaultValue = "0") int start){
        String matcher=map.get("matcher");
        String orderBy=map.get("orderBy");
        matcher=matcher==null?"":matcher;
        /*记录列表*/
        List<OpenDeveloper> list=developerService.query(matcher,orderBy,start);
        /*总页数*/
        int pageCount=developerService.getPageCount(matcher);
        /*满足条件的记录总数*/
        long count=developerService.count(matcher);
        return Response.SUCCESS().put("result",list).put("count",count).put("pageCount",pageCount).toJson();
    }


    /**
     * 添加ip白名单
     * @param partner 开发者账号
     * @param ips 白名单列表
     * @return
     */
    @RequestMapping("/addIps")
    @ResponseBody
    public Object addIps(String partner,String ips){
        developerService.addIps(partner, ips);
        return Response.SUCCESS("添加成功").toJson();
    }


    /**
     * 删除ip白名单
     * @param partner 开发者账号
     * @param ips 白名单列表
     * @return
     */
    @RequestMapping("/deleteIps")
    @ResponseBody
    public Object deleteIps(String partner,String ips){
        developerService.deleteIps(partner, ips);
        return Response.SUCCESS("删除成功").toJson();
    }

    /**
     * 删除指定开发者指定接口的权限
     * @param partner 开发者账号
     * @param method 接口标识
     * @return
     */
    @RequestMapping("/deletePermission")
    @ResponseBody
    public Object deletePermission(String partner,String method){
        developerService.deletePermission(partner, method);
        return Response.SUCCESS("删除成功").toJson();
    }


    /**
     * 为某个开发者增加权限
     * @param partner 开发者账号
     * @param permission 权限配置
     */
    @RequestMapping("/addPermission")
    @ResponseBody
    public Object addPermission(String partner,OpenPermission permission){
        developerService.addPermission(partner, permission);
        return Response.SUCCESS("添加成功").toJson();
    }

    /**
     * 修改某个开发者的某个接口的权限
     * @param partner 开发者账号
     * @param method 接口标识
     * @param permission 需要修改的属性
     */
    @RequestMapping("/updatePermission")
    @ResponseBody public Object updatePermission(String partner,String method,OpenPermission permission){
        developerService.updatePermission(partner, method, permission);
        return Response.SUCCESS("修改成功").toJson();
    }


    /**
     * 查询指定开发者拥有的接口权限列表
     * @param partner 开发者账号
     * @return
     */
    @RequestMapping("/getPermission")
    @ResponseBody public Object getPermission(String partner,@RequestParam(value = "method",required = false)  String method){
        List<OpenPermission> list=developerService.getPermission(partner,method);
        return Response.SUCCESS().put("result",list).toJson();
    }

    @ResponseBody
    @RequestMapping("/delExtParam")
    public Object delExtParam(String partner,String extName){
        developerService.delExtParam(partner,extName);
        return Response.SUCCESS("删除成功").toJson();
    }

    @ResponseBody
    @RequestMapping("/updateExtParam")
    public Object updateExtParam(String partner,@RequestParam(required = false)  Map<String,String> map){
        developerService.updateExtParam(partner,map);
        return Response.SUCCESS("更新成功").toJson();
    }

    @ResponseBody
    @RequestMapping("/getExtParam")
    public Object getExtParam(String partner){
        return Response.SUCCESS("查询成功").put("result",developerService.getExtParam(partner)).toJson();
    }
    
    @ResponseBody
    @RequestMapping("/checkIsExist")
    public boolean isShortNameExist(@RequestParam(value = "shortName") String shortName){
        return developerService.findOneByShortName(shortName) == null ? true :false;
    }
    
}
